package com.kaiwukj.android.ufamily.mvp.ui.page.keeper;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpFragment;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerKeeperComponent;
import com.kaiwukj.android.ufamily.di.module.KeeperModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class KeeperBindFragment extends BaseMvpFragment<KeeperPresenter> implements KeeperContract.View {


    @BindView(R.id.container_pick)
    ViewGroup containerPick;
    @BindView(R.id.container_register_customer)
    ViewGroup containerRegisterCustomer;
    @BindView(R.id.et_code)
    EditText etCode;

    private int type = 0;

    @OnClick(R.id.btn_bind)
    void intent2Bind() {
        start(KeeperBindFragment.newInstance(1));
    }

    @OnClick(R.id.btn_pick)
    void intent2Picker() {
        ARouter.getInstance().build(ARouterUrlKt.KeeperPickUrl)
                .navigation();
    }

    @OnClick(R.id.btn_confirm)
    void bindByCode() {
        String code = etCode.getText().toString();
        if (StringUtils.isEmpty(code)) {
            showMessage("绑定内容不能为空");
            return;
        }
        mPresenter.bindKeeper(code, false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_keeper_bind;
    }

    public static KeeperBindFragment newInstance(int type) {
        KeeperBindFragment fragment = new KeeperBindFragment();
        fragment.type = type;
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerKeeperComponent.builder()
                .appComponent(appComponent)
                .keeperModule(new KeeperModule(this))
                .build().inject(this);
    }

    @Override
    protected void initView() {
        if (type == 0) {
            containerPick.setVisibility(View.VISIBLE);
            containerRegisterCustomer.setVisibility(View.GONE);
        } else {
            containerPick.setVisibility(View.GONE);
            containerRegisterCustomer.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void showKeeperInfo(KeeperResult result) {

    }

    @Override
    public void showKeeperList(List<KeeperResult> results) {

    }

    @Override
    public void showKeeperOderList(List<KeeperOrderResult> results) {

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (KeeperPresenter.BIND == taskId) {
            ARouter.getInstance().build(ARouterUrlKt.KeeperIndexUrl)
                    .navigation(getContext(), new NavigationCallback() {
                        @Override
                        public void onFound(Postcard postcard) {

                        }

                        @Override
                        public void onLost(Postcard postcard) {

                        }

                        @Override
                        public void onArrival(Postcard postcard) {
                            if (getActivity() != null) {
                                getActivity().finish();
                            }
                        }

                        @Override
                        public void onInterrupt(Postcard postcard) {

                        }
                    });
        }
    }
}
